﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Dycox.Drawing
{
    public static class GdiHelpers
    {
        public static Rectangle Deflate(this Rectangle rect, int top, int right, int bottom, int left)
        {
            Rectangle rtv = new Rectangle(
            rect.X + left,
            rect.Y + top,
            rect.Width - left  - right,
            rect.Height - top - bottom);

            return rtv;
        }

        public static GraphicsPath CreateRoundedCornerPath(this Rectangle rect, float radius)
        {
            return CreateRoundedCornerPath(rect.X, rect.Y, rect.Width - 1, rect.Height - 1, radius);
        }

        public static GraphicsPath CreateRoundedCornerPath(this RectangleF rect, float radius)
        {
            return CreateRoundedCornerPath(rect.X, rect.Y, rect.Width - 1, rect.Height - 1, radius);
        }

        public static GraphicsPath CreateRoundedCornerPath(float x, float y, float width, float height, float radius)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.StartFigure();
            gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner
            gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
            gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner
            gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
            gp.CloseFigure();

            return gp;
        }
    }
}
